package com.clientmanage.service.impl;

import com.clientmanage.domain.bean.ServiceEvaluation;
import com.clientmanage.domain.vo.serviceEvaluationVo;
import com.clientmanage.domain.resp.PageResult;
import com.clientmanage.mapper.ServiceEvaluationMapper;
import com.clientmanage.service.IServiceEvaluationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.time.LocalDateTime;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author HUANG
 * @since 2025-06-30
 */
@Service
public class ServiceEvaluationServiceImpl extends ServiceImpl<ServiceEvaluationMapper, ServiceEvaluation> implements IServiceEvaluationService {

    @Autowired
    private ServiceEvaluationMapper serviceEvaluationMapper;

    /**
     * 分页模糊查询服务评价
     */
    @Override
    public PageResult<serviceEvaluationVo> pageQuery(String serviceName, Integer score, String date, int pageNum, int pageSize) {
        int offset = (pageNum - 1) * pageSize;
        List<serviceEvaluationVo> list = serviceEvaluationMapper.selectPageVo(serviceName, score, date, pageSize, offset);
        return new PageResult<>((long) pageSize, (long) pageNum, list, (long) list.size());
    }

    /**
     * 新增或更新服务评价
     */
    @Transactional
    @Override
    public void saveOrUpdateServiceEvaluation(serviceEvaluationVo vo) {
        ServiceEvaluation entity = new ServiceEvaluation();
        org.springframework.beans.BeanUtils.copyProperties(vo, entity);
        entity.setReviewTime(java.time.LocalDateTime.now());
        this.saveOrUpdate(entity);
    }

    /**
     * 删除服务评价
     */
    @Transactional
    @Override
    public void deleteById(String id) {
        this.removeById(id);
    }

    /**
     * 统计服务评价分数和数量
     */
    @Override
    public Map<String, Object> getEvaluationStats(String serviceName, String date) {
        return serviceEvaluationMapper.getEvaluationStats(serviceName, date);
    }
}
